package com.zcyppyh.ddns.host.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zcyppyh.ddns.enums.ProtocolTypeEnum;
import com.zcyppyh.ddns.host.PublicHost;
import org.springframework.stereotype.Service;

/**
 * @author: zcyppyh
 * @time: 2023/8/16 18:13
 * @version: 1
 * @desc:
 */
@Service("Ipv4Url1PublicHostImpl")
public class Ipv4Url1PublicHostImpl implements PublicHost {

    public static final String URL = "http://ipv4.lookup.test-ipv6.com/ip/?callback=_jqjsp&asn=1&testdomain=test-ipv6.com&testname=test_asn4";

    @Override
    public String getPublicIp() {
        String result = HttpUtil.get(URL);
        result = result.substring(result.indexOf("{"));
        result = result.substring(0, result.length()-1);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        String ip = jsonObject.get("ip", String.class);
        return ip;
    }

    @Override
    public ProtocolTypeEnum protocolType() {
        return ProtocolTypeEnum.IPV4;
    }

    @Override
    public boolean isCustImpl() {
        return false;
    }
}
